/*
 * 浮点类型转换器
 */
package com.slipper.common.excel.parse.support;

import com.slipper.common.excel.ExcelColumn;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;

public class DateParse extends CellParse {
    @Override
    public Object parse(Cell cell, ExcelColumn column) throws Exception {
        // 判断是否为null或空串
        if (cell == null || cell.toString().trim().isEmpty()) {
            return null;
        }
        
        Object cellValue;
        CellType cellType = cell.getCellType();
        switch (cellType) {
            case NUMERIC: // 数字
                short format = cell.getCellStyle().getDataFormat();
                if (DateUtil.isCellDateFormatted(cell)) {
                    if (format == 14 || format == 31 || format == 57 || format == 58) {
                        double value = cell.getNumericCellValue();
                        cellValue = DateUtil.getJavaDate(value);
                    } else {
                        cellValue = cell.getDateCellValue(); // 日期
                    }
                } else {
                    BigDecimal bd = BigDecimal.valueOf(cell.getNumericCellValue());
                    cellValue = new Date(bd.longValue());
                }
                break;
            case STRING: // 字符串
                try {
                    cellValue = com.slipper.common.util.DateUtil.parseDate(cell.getStringCellValue());
                } catch(Exception e) {
                    throw new Exception("日期格式不正确！");
                }
                break;
            default:
                throw new Exception("日期类型不正确！");
        }
        return cellValue;
    }
}
